package com.canbot.u05.activity.attendance;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.button.MaterialButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.TimePickerDialog;
import com.canbot.u05.widget.LoadingDialogUtils;
import java.util.HashMap;

/**
 * 考勤信息显示界面  to_work_time   off_work_time  值 “09:00”
 */
public class AttendanceSettingTimeActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private static final String TAG = "AttendanceSettingTimeActivity";

        private String[] fileName = new String[]{"u05_ kqgz_001.wav", "u05_ kqgz_002.wav", "u05_ kqgz_003.wav"};

        private String amToWorkTime;

        private String amOffWorkTime;

        private String pmToWorkTime;

        private String pmOffWorkTime;

        private ImageView btnBack;

        private MaterialButton btnSure;

        private Button btnWeek1, btnWeek2, btnWeek3, btnWeek4, btnWeek5, btnWeek6, btnWeek7;

        private TextView  tvAmToWorkTime;

        private TextView  tvAmOffWorkTime;

        private TextView  tvPmToWorkTime;

        private TextView  tvPmOffWorkTime;

        private String nonWorkingDay = "一:二:三:四:五:六:日";

        private Dialog dialogLoading;

        private TimePickerDialog timePickerDialog;

        private String timePickerTime;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_attendance_info);

                getDefaultSetting();
                setViews();
                setListener();
                defaultSetting();
                int index = (int) (Math.random() * fileName.length);
                boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.AUDIO_ATTENDANCE_DIR + fileName[index]);
                if (!isSuccess) {
                        ToastTools.newInstance(this).showText(getString(R.string.network_error));
                }

        }

        /**
         * 用户考勤信息显示界面
         */
        private void setViews() {
                btnBack = (ImageView) findViewById(R.id.imageView_back);
                btnSure = (MaterialButton) findViewById(R.id.btn_sure);

                btnWeek1 = (Button) findViewById(R.id.week_1);
                btnWeek2 = (Button) findViewById(R.id.week_2);
                btnWeek3 = (Button) findViewById(R.id.week_3);
                btnWeek4 = (Button) findViewById(R.id.week_4);
                btnWeek5 = (Button) findViewById(R.id.week_5);
                btnWeek6 = (Button) findViewById(R.id.week_6);
                btnWeek7 = (Button) findViewById(R.id.week_7);

                tvAmToWorkTime = (TextView) findViewById(R.id.am_to_work_time);

                tvAmOffWorkTime = (TextView) findViewById(R.id.am_off_work_time);

                tvPmToWorkTime = (TextView) findViewById(R.id.pm_to_work_time);

                tvPmOffWorkTime = (TextView) findViewById(R.id.pm_off_work_time);

        }

        private void setListener(){
                btnBack.setOnClickListener(this);
                btnSure.setOnClickListener(this);

                btnWeek1.setOnClickListener(this);
                btnWeek2.setOnClickListener(this);
                btnWeek3.setOnClickListener(this);
                btnWeek4.setOnClickListener(this);
                btnWeek5.setOnClickListener(this);
                btnWeek6.setOnClickListener(this);
                btnWeek7.setOnClickListener(this);

                tvAmToWorkTime.setOnClickListener(this);
                tvAmOffWorkTime.setOnClickListener(this);
                tvPmToWorkTime.setOnClickListener(this);
                tvPmOffWorkTime.setOnClickListener(this);
        }

        private void defaultSetting(){
                tvAmToWorkTime.setText(amToWorkTime);
                tvAmOffWorkTime.setText(amOffWorkTime);
                tvPmToWorkTime.setText(pmToWorkTime);
                tvPmOffWorkTime.setText(pmOffWorkTime);

                if ("一:二:三:四:五:六:日".equals(nonWorkingDay)){
                        btnWeek1.setSelected(true);
                        btnWeek2.setSelected(true);
                        btnWeek3.setSelected(true);
                        btnWeek4.setSelected(true);
                        btnWeek5.setSelected(true);
                        btnWeek6.setSelected(true);
                        btnWeek7.setSelected(true);
                }else {
                        String[] str = nonWorkingDay.split(":");
                        for (int i=0;i<str.length;i++){
                                switch (str[i]){
                                        case "一":
                                                btnWeek1.setSelected(true);
                                                break;
                                        case "二":
                                                btnWeek2.setSelected(true);
                                                break;
                                        case "三":
                                                btnWeek3.setSelected(true);
                                                break;
                                        case "四":
                                                btnWeek4.setSelected(true);
                                                break;
                                        case "五":
                                                btnWeek5.setSelected(true);
                                                break;
                                        case "六":
                                                btnWeek6.setSelected(true);
                                                break;
                                        case "日":
                                                btnWeek7.setSelected(true);
                                                break;
                                }
                        }
                }
        }

        private void getDefaultSetting() {
                amToWorkTime = SPUtils.getString(this, "am_to_work", "09:00");
                amOffWorkTime = SPUtils.getString(this, "am_off_work", "12:00");
                pmToWorkTime = SPUtils.getString(this, "pm_to_work", "13:30");
                pmOffWorkTime = SPUtils.getString(this, "pm_off_work", "18:00");

                nonWorkingDay = SPUtils.getString(this,"non_working_day","一:二:三:四:五:六:日");
                Logger.e(TAG, "获取初始化非工作日 = " + nonWorkingDay);
        }

        /**
         * 加载显示考勤信息
         */
        private void commitData() {

                amToWorkTime = tvAmToWorkTime.getText().toString().trim();
                pmOffWorkTime = tvPmOffWorkTime.getText().toString().trim();
                pmToWorkTime = tvPmToWorkTime.getText().toString().trim();
                amOffWorkTime = tvAmOffWorkTime.getText().toString().trim();
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.data_upload));
                HashMap<String, Object> hasMap = new HashMap<>();
                hasMap.put("am_to_work", amToWorkTime);
                hasMap.put("pm_off_work", pmOffWorkTime);
                hasMap.put("pm_to_work", pmToWorkTime);
                hasMap.put("am_off_work", amOffWorkTime);
                hasMap.put("non_working_day", nonWorkingDay);

                Logger.e(TAG, "提交数据 时间 =  " + amToWorkTime + "  " + amOffWorkTime + "  " + pmToWorkTime + "  " + pmOffWorkTime);
                Logger.e(TAG, "非工作日 = " + nonWorkingDay);
                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "settingAttendanceTime", hasMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {

                                Logger.e("save", "onSuccess:" + result);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                ToastTools.newInstance(AttendanceSettingTimeActivity.this).showText(AttendanceSettingTimeActivity.this.getString(R.string.upload_complete));
                                                if (!TextUtils.isEmpty(amToWorkTime)) {

                                                        SPUtils.saveString(AttendanceSettingTimeActivity.this, "am_to_work", amToWorkTime);
                                                }
                                                if (!TextUtils.isEmpty(pmOffWorkTime)) {

                                                        SPUtils.saveString(AttendanceSettingTimeActivity.this, "pm_off_work", pmOffWorkTime);
                                                }
                                                if (!TextUtils.isEmpty(amOffWorkTime)) {

                                                        SPUtils.saveString(AttendanceSettingTimeActivity.this, "am_off_work", amOffWorkTime);
                                                }
                                                if (!TextUtils.isEmpty(pmToWorkTime)) {

                                                        SPUtils.saveString(AttendanceSettingTimeActivity.this, "pm_to_work", pmToWorkTime);
                                                }
                                                if (!TextUtils.isEmpty(nonWorkingDay)) {
                                                        if (nonWorkingDay.startsWith(":")){
                                                                nonWorkingDay = nonWorkingDay.substring(1,nonWorkingDay.length());
                                                        }
                                                        if (nonWorkingDay.endsWith(":")){
                                                                nonWorkingDay = nonWorkingDay.substring(0,nonWorkingDay.length() - 1);
                                                        }

                                                        Logger.e(TAG, "non_working_day = " +nonWorkingDay);
                                                }
                                                SPUtils.saveString(AttendanceSettingTimeActivity.this, "non_working_day", nonWorkingDay);
                                                //跳转考勤库选择界面
                                                startActivity(new Intent(AttendanceSettingTimeActivity.this, AttendanceGroupActivity.class));
                                                finish();
                                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                ToastTools.newInstance(AttendanceSettingTimeActivity.this).showText(AttendanceSettingTimeActivity.this.getString(R.string.network_error));
                                        }
                                });
                        }
                });
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.btn_sure:

                                if (btnWeek1.isSelected() && btnWeek2.isSelected() && btnWeek3.isSelected() && btnWeek4.isSelected() && btnWeek5.isSelected() && btnWeek6.isSelected() && btnWeek7.isSelected()) {
                                        ToastTools.newInstance(AttendanceSettingTimeActivity.this).showText("工作日设置不能为空哦！");
                                        return;
                                }
                                commitData();
                                break;
                        case R.id.week_1:
                                if (btnWeek1.isSelected()) {
                                        btnWeek1.setSelected(false);
                                        setNonWorkingDay(false, "一");
                                }
                                else {
                                        btnWeek1.setSelected(true);
                                        setNonWorkingDay(true, "一");
                                }
                                break;
                        case R.id.week_2:
                                if (btnWeek2.isSelected()) {
                                        btnWeek2.setSelected(false);
                                        setNonWorkingDay(false, "二");
                                        //去除
                                }
                                else {
                                        btnWeek2.setSelected(true);
                                        setNonWorkingDay(true, "二");
                                }
                                break;
                        case R.id.week_3:
                                if (btnWeek3.isSelected()) {
                                        btnWeek3.setSelected(false);
                                        setNonWorkingDay(false, "三");
                                }
                                else {
                                        btnWeek3.setSelected(true);
                                        setNonWorkingDay(true, "三");
                                }
                                break;
                        case R.id.week_4:
                                if (btnWeek4.isSelected()) {
                                        btnWeek4.setSelected(false);
                                        setNonWorkingDay(false, "四");
                                }
                                else {
                                        btnWeek4.setSelected(true);
                                        setNonWorkingDay(true, "四");
                                }
                                break;
                        case R.id.week_5:
                                if (btnWeek5.isSelected()) {
                                        btnWeek5.setSelected(false);
                                        setNonWorkingDay(false, "五");
                                }
                                else {
                                        btnWeek5.setSelected(true);
                                        setNonWorkingDay(true, "五");
                                }
                                break;
                        case R.id.week_6:
                                if (btnWeek6.isSelected()) {
                                        btnWeek6.setSelected(false);
                                        setNonWorkingDay(false, "六");
                                }
                                else {
                                        btnWeek6.setSelected(true);
                                        setNonWorkingDay(true, "六");
                                }
                                break;
                        case R.id.week_7:
                                if (btnWeek7.isSelected()) {
                                        btnWeek7.setSelected(false);
                                        setNonWorkingDay(false, "日");
                                        Logger.e(TAG,"R.id.week_7 false 绿色");
                                }
                                else {
                                        btnWeek7.setSelected(true);
                                        setNonWorkingDay(true, "日");
                                        Logger.e(TAG,"R.id.week_7 true 灰色");
                                }
                                break;

                        case R.id.am_to_work_time:
                                String amTo = tvAmToWorkTime.getText().toString();
                                showTimePicker(getDefaultHour(amTo),getDefaultMin(amTo),tvAmToWorkTime);

                                break;
                        case R.id.am_off_work_time:
                                String amOff = tvAmOffWorkTime.getText().toString();
                                showTimePicker(getDefaultHour(amOff),getDefaultMin(amOff),tvAmOffWorkTime);

                                break;
                        case R.id.pm_to_work_time:
                                String pmTo = tvPmToWorkTime.getText().toString();
                                showTimePicker(getDefaultHour(pmTo),getDefaultMin(pmTo),tvPmToWorkTime);

                                break;
                        case R.id.pm_off_work_time:
                                String pmOff = tvPmOffWorkTime.getText().toString();
                                showTimePicker(getDefaultHour(pmOff),getDefaultMin(pmOff),tvPmOffWorkTime);

                                break;
                }
        }

        private void showTimePicker(int hour,int minute,final TextView textView){

                timePickerDialog = new TimePickerDialog(AttendanceSettingTimeActivity.this);

                timePickerDialog.setTitle("设置时间");
                timePickerDialog.setTime(hour,minute);
                timePickerDialog.setOnNegativeListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                timePickerDialog.dismiss();

                        }
                });
                timePickerDialog.setOnPositiveListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {
                                timePickerTime = timePickerDialog.getTime();
                                Logger.e(TAG,"设置时间= " +timePickerTime);
                                timePickerDialog.dismiss();
                                textView.setText(timePickerTime);
                        }
                });
                timePickerDialog.show();
        }

        private int getDefaultHour(String defaultTime) {
                final String[] stramToWorkTime = defaultTime.split(":");
                if (stramToWorkTime[0].startsWith("0")) {
                        final String time = String.valueOf(stramToWorkTime[0].charAt(1));
                        return Integer.parseInt(time);
                }
                else {
                        return Integer.parseInt(stramToWorkTime[0]);
                }
        }

        private int getDefaultMin(String defaultTime) {
                final String[] stramToWorkTime = defaultTime.split(":");
                if (stramToWorkTime[1].startsWith("0")) {
                        final String time = String.valueOf(stramToWorkTime[1].charAt(1));
                        return Integer.parseInt(time);
                }
                else {
                        return Integer.parseInt(stramToWorkTime[1]);
                }
        }

        private void setNonWorkingDay(boolean flag, String week) {
                nonWorkingDay = nonWorkingDay.trim();
                if (flag) {

                        if (nonWorkingDay.endsWith(":")) {
                                nonWorkingDay = nonWorkingDay + week + ":";
                        }
                        else {
                                nonWorkingDay = nonWorkingDay + ":" + week + ":";
                        }
                }
                else {
                        if (nonWorkingDay.trim().contains(week)) {
                                if (nonWorkingDay.endsWith(week)){
                                        nonWorkingDay = nonWorkingDay.replace(week , "");
                                }else {
                                        nonWorkingDay = nonWorkingDay.replace(week + ":", "");
                                }
                        }
                }
                Logger.e(TAG, "setNonWorkingDay nonWorkingDay = " + nonWorkingDay + "week " + week);
        }


        private void increaseTime(TextView textView) {

                String originalTime = textView.getText().toString().trim();
                String[] timeSplit = originalTime.split(":");
                String hourString = timeSplit[0];
                String minuteString = timeSplit[1];
                int hour;
                int minute;
                if (hourString.startsWith("0")) {
                        hour = Integer.parseInt(String.valueOf(hourString.charAt(1)));
                }
                else {
                        hour = Integer.parseInt(hourString);
                }
                if (minuteString.startsWith("0")) {
                        minute = Integer.parseInt(String.valueOf(minuteString.charAt(1)));
                }
                else {
                        minute = Integer.parseInt(minuteString);
                }
                if (minute >= 59) {
                        if (hour >= 23) {
                                hour = 0;
                        }
                        else {
                                //hour+1
                                hour = hour + 1;
                        }
                        minute++;
                        minute = 0;
                }
                else {
                        minute++;
                }
                String finalHour = "";
                String finalMinute = "";

                if (hour < 10) {
                        finalHour = "0" + hour;
                }
                else {
                        finalHour = hour + "";
                }
                if (minute < 10) {
                        finalMinute = "0" + minute;
                }
                else {
                        finalMinute = minute + "";
                }
                textView.setText(finalHour + ":" + finalMinute);
        }


        @Override
        protected void onPause() {
                if (dialogLoading != null && dialogLoading.isShowing()){
                        LoadingDialogUtils.closeDialog(dialogLoading);
                }
                super.onPause();
        }

        @Override
        protected void onDestroy() {
                if (timePickerDialog != null){
                        timePickerDialog.dismiss();
                        timePickerDialog = null;
                }
                super.onDestroy();

        }
}
